/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.sll.engagemangsindex.ws;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import se.sll.engagemangsindex.entity.Engagement;
import java.util.Collection;
import javax.jws.WebService;
import javax.xml.soap.SOAPException;
import lombok.extern.slf4j.Slf4j;
import riv.itintegration.engagementindex._1.EngagementType;
import riv.itintegration.engagementindex.findcontent._1.rivtabp21.FindContentResponderInterface;
import riv.itintegration.engagementindex.findcontentresponder._1.FindContentResponseType;
import riv.itintegration.engagementindex.findcontentresponder._1.FindContentType;

/**
 *
 * @author khaleddaham
 */
@WebService(endpointInterface = "riv.itintegration.engagementindex.findcontent._1.rivtabp21.FindContentResponderInterface",
serviceName = "FindContent")
@Slf4j
public class FindContentWSImpl extends BaseUpdateImpl implements FindContentResponderInterface {

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");

    /**
     *
     */
    public FindContentWSImpl() {
        super();
    }

    /**
     *
     * @param logicalAddress
     * @param parameters
     * @return
     * @throws SOAPException
     */
    @Override
    public FindContentResponseType findContent(String logicalAddress, FindContentType parameters) {
        FindContentResponseType response = new FindContentResponseType();

        if ((parameters.getRegisteredResidentIdentification() == null) && (parameters.getClinicalProcessInterestId() != null)) {
            this.throwSoapFaultException("ClinicalProcessInterestId may not be set when RegisteredIdentifiationId is not set.");
        }

        if (parameters.getServiceDomain() == null) {
            if (parameters.getCategorization() != null) {
                this.throwSoapFaultException("Categorization may not be set when ServiceDomain is not set.");

            }
            if (parameters.getLogicalAddress() != null) {
                this.throwSoapFaultException("LogicalAddress may not be set when ServiceDomain is not set.");

            }
            if (parameters.getBusinessObjectInstanceIdentifier() != null) {
                this.throwSoapFaultException("BusinessObjectInstanceIdentifier may not be set when ServiceDomain is not set.");

            }
            if (parameters.getMostRecentContent() != null) {
                this.throwSoapFaultException("MostRecentContent may not be set when ServiceDomain is not set.");
            }
        }

        Collection<Engagement> engagementList = null;
        try {
            engagementList = getEngagementEjb().findContent(
                    parameters.getRegisteredResidentIdentification(),
                    parameters.getServiceDomain(),
                    parameters.getCategorization(),
                    parameters.getLogicalAddress(),
                    parameters.getBusinessObjectInstanceIdentifier(),
                    parameters.getClinicalProcessInterestId(),
                    parameters.getMostRecentContent(),
                    parameters.getSourceSystem(),
                    parameters.getOwner());
        } catch (ParseException ex) {
            log.warn("Failed to fetch content: ", ex);
            this.throwSoapFaultException(ex.getMessage());
        }
        if (engagementList == null) {
            log.error("Returned list of engagements are null.");
            this.throwSoapFaultException("Returned list of engagements are null");
        }
        return buildResponse(response, engagementList);
    }

    /**
     *
     * @param response
     * @param engagementList
     * @return
     */
    protected FindContentResponseType buildResponse(FindContentResponseType response, Collection<Engagement> engagementList) {
        for (Engagement eData : engagementList) {
            EngagementType eType = new EngagementType();
            eType.setBusinessObjectInstanceIdentifier(eData.getBusinessObjectInstanceIdentifier());
            eType.setCategorization(eData.getCategorization());
            eType.setClinicalProcessInterestId(eData.getClinicalProcessInterestId());
            eType.setLogicalAddress(eData.getLogicalAddress());
            eType.setOwner(eData.getOwner());
            eType.setRegisteredResidentIdentification(eData.getRegisteredResidentIdentification());
            eType.setServiceDomain(eData.getServiceDomain());
            eType.setSourceSystem(eData.getSourceSystem());
            eType.setUpdateTime(DATE_FORMAT.format(eData.getUpdateTime()).toString());
            eType.setCreationTime(DATE_FORMAT.format(eData.getCreationTime()).toString());
            response.getEngagement().add(eType);
        }
        return response;
    }
}
